#include "localVersion.h"
#include <Windows.h>
#include "stringUtils.h"

#pragma comment(lib, "version.lib")

String loadVersion() {
	TCHAR pFilePath[MAX_PATH] = { 0 };
	DWORD dwRet = GetModuleFileName(NULL, pFilePath, MAX_PATH);
	LPVOID lpBuffer = NULL;
	UINT uLen = 0;
	String path = L"\\StringFileInfo\\080404b0\\FileVersion";

	if (dwRet == 0)
	{
		return L"";
	}

	DWORD dwSize = GetFileVersionInfoSize(pFilePath, NULL);
	if (dwSize == 0)
	{
		return L"";
	}
	TCHAR* pBuf = (TCHAR*)malloc(dwSize + 1);
	if (!pBuf) {
		return L"";
	}
	memset(pBuf, 0, dwSize + 1);
	dwRet = GetFileVersionInfo(pFilePath, NULL, dwSize, pBuf);
	if (dwRet == 0)
	{
		goto ret;
	}

	dwRet = VerQueryValue(pBuf, path.c_str(), &lpBuffer, &uLen);
	if (dwRet == 0)
	{
		goto ret;
	}
	//StringUtils::Wstring2string((TCHAR*)lpBuffer, buf, 20);
ret:
	String ret;
	if (lpBuffer) {
		ret = (TCHAR * )lpBuffer;
	}
	if (pBuf) {
		free(pBuf);
	}
	return ret;
}

String LocalVersion::exeVersion()
{
	static String version = loadVersion();
	return version;
}
